import { createRouter, createWebHistory } from 'vue-router'
import { useCounterStore } from '../stores/counter'
import { ref } from 'vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/Login.vue')
    },
    {
      path: '/wxlogin',
      name: 'wxlogin',
      component: () => import('../views/WxLogin.vue')
    },
    {
      path: '/:data',
      name: 'main',
      component: () => import('../views/Main.vue'),
    },
    {
      path: '/word',
      name: 'word',
      component: () => import('../views/Word.vue')
    },
    {
      path: '/excel',
      name: 'excel',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/Excel.vue')
    },
    {
      path: '/dataset',
      name: 'dataset',
      component: () => import('../views/DataSet.vue')

    },
    {
      path: '/application',
      name: 'application',
      
      component: () => import('../views/MyApp.vue')
    },
    {
      path: '/aiapp',
      name: 'aiapp',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/AiApp.vue')
    },
    {
      path: '/meeting',
      name: 'meeting',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/meeting.vue')
    },
    {
      path: '/日程',
      name: '日程',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/我的日程.vue')
    },
    {
      path: '/wxxc',
      name: 'wxxc',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/小程序.vue')
    },
    {
      path: '/setting',
      name: 'setting',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/我的设置.vue')
    },
    {
      path: '/3d',
      name: '3d',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/3D.vue')
    },
  ]
})
router.beforeEach((to, from, next) => {
  const isAuthenticated = ref<Boolean>(false)
  const counterStore = useCounterStore()
  isAuthenticated.value = counterStore.isLogin
  console.log('路由跳转', to, from, isAuthenticated.value)
  if(to.name === 'wxlogin') next() // 拦截 微信扫码登陆的路由跳转地址
  else if (to.name !== 'login' && !isAuthenticated.value) next({ name: 'login' })
  else next()
})
export default router
